//
//  TTITextField.m
//  iOSCodeProject
//
//  Created by Fox on 14-7-18.
//  Copyright (c) 2014年 翔傲信息科技（上海）有限公司. All rights reserved.
//

#import "TTITextField.h"

@interface TTITextField ()
- (void)initialize;
@end


@implementation TTITextField
@synthesize textEdgeInsets = _textEdgeInsets;
@synthesize clearButtonEdgeInsets = _clearButtonEdgeInsets;
@synthesize placeholderTextColor = _placeholderTextColor;


- (void)setPlaceholderTextColor:(UIColor *)placeholderTextColor {
	_placeholderTextColor = placeholderTextColor;
	
	if (!self.text && self.placeholder) {
		[self setNeedsDisplay];
	}
}


#pragma mark - Init
- (id)initWithCoder:(NSCoder *)aDecoder {
    if ((self = [super initWithCoder:aDecoder])) {
        [self initialize];
    }
    return self;
}


- (id)initWithFrame:(CGRect)frame {
    if ((self = [super initWithFrame:frame])) {
        [self initialize];
    }
    return self;
}

- (void)initialize {
	_textEdgeInsets = UIEdgeInsetsZero;
	_clearButtonEdgeInsets = UIEdgeInsetsZero;
}

#pragma mark - UITextField

- (CGRect)textRectForBounds:(CGRect)bounds {
	return UIEdgeInsetsInsetRect([super textRectForBounds:bounds], _textEdgeInsets);
}


- (CGRect)editingRectForBounds:(CGRect)bounds {
	return [self textRectForBounds:bounds];
}


- (CGRect)clearButtonRectForBounds:(CGRect)bounds {
    
    //删除按钮的位置
	CGRect rect = [super clearButtonRectForBounds:bounds];
    rect.origin.y = rect.origin.y + _clearButtonEdgeInsets.top;
    rect.origin.x = rect.origin.x + _clearButtonEdgeInsets.right;
    
	return rect;
}


- (void)drawPlaceholderInRect:(CGRect)rect {
	
    if (!_placeholderTextColor) {
		[super drawPlaceholderInRect:rect];
		return;
	}
	
    [_placeholderTextColor setFill];
#if __IPHONE_OS_VERSION_MIN_REQUIRED >= __IPHONE_6_0
    rect.origin.y += (rect.size.height - self.font.pointSize)/2;
    [self.placeholder drawInRect:rect
                        withFont:self.font
                   lineBreakMode:NSLineBreakByTruncatingTail
                       alignment:self.textAlignment];
#else
    [self.placeholder drawInRect:rect
                        withFont:self.font
                   lineBreakMode:NSLineBreakByTruncatingTail
                       alignment:self.textAlignment];
#endif
}

@end
